import { ys, ysUtils } from "ys-ts";
import App from "../../components/layouts/app/Index.vue";
import Home from "../../components/pages/Home.vue";
import { RouteLocationNormalized } from "vue-router";
import { useLayoutStore } from "../store";
import { cfgSqlHome, initSql } from "./sql";
import Test from "../../components/pages/Test.vue";

export let nameApp: string;
export let cfgApp: ysUtils.YsRouteRecord;

export let nameHome: string;
export let cfgHome: ysUtils.YsRouteRecord;

export let nameTest: string;
export let cfgTest: ysUtils.YsRouteRecord;

export function selectOrAddTab(to: RouteLocationNormalized): boolean {
  const tab = useLayoutStore();

  if (to.meta.useTab) {
    tab.addTabByRoute(to);
    tab.current = <string>to.name;
  } else {
    tab.current = "";
  }

  return true;
}

export function initApp() {
  console.log(`[router/app] 初始化！`);

  nameApp = "app";
  cfgApp = ys.getYsRouterRecord(nameApp, {
    path: "/",
    name: nameApp,
    component: App,
    meta: { useTab: true },
  });

  nameHome = "home";
  cfgHome = ys.getYsRouterRecord(nameHome, {
    path: "/",
    name: nameHome,
    component: Home,
  });
  cfgApp.addChildren(cfgHome);

  nameTest = "test";
  cfgTest = ys.getYsRouterRecord(nameTest, {
    path: "/test",
    name: nameTest,
    component: Test,
  });
  cfgApp.addChildren(cfgTest);

  initSql();
  cfgApp.addChildren(cfgSqlHome);
}
